home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / stzcr < prev    next >
Text File  |  1995-03-20  |  2KB  |  92 lines

  1. %SP_STZCR Short-time zero crossings.
  2.  
  3. %       [Y,TSCALE] = SP_STZCR(X,FRAME,OVERLAP,FS) computes the
  4. %       short-time zero-crossing rate of X using a frame size
  5. %       of LENGTH and a percentage OVERLAP between successive
  6. %       frames using a rectangular data window.  The sampling
  7. %       frequency is given by FS. The short-time zero-crossing
  8. %       curve is returned in Y and a time scale corresponding
  9. %       to the end of the data frame is returned in TSCALE.
  10. %       The curve may be displayed with the command
  11. %       'plot(y,tscale)'.
  12. %
  13. %       See also: STMAG, STENG, AVSMOOTH, MDSMOOTH
  14.  
  15. %       Matlab original by:
  16. %       LT Dennis W. Brown 7-11-93, DWB 11-11-94
  17. %       Naval Postgraduate School, Monterey, CA
  18. %       May be freely distributed.
  19. %       Not for use in commercial products.
  20.  
  21. %       Ref: Rabiner & Schafer, Digital Processing of Speech
  22. %       Signals, 1978, ss 4.3, pp 127-130.
  23.  
  24. % window argument is not used but is here to maintain consistency with
  25. % the other sp_ routines.
  26.  
  27. static (sgn)
  28.  
  29. stzcr = function (x, frame, overlap, fs, win)
  30. {
  31.   local (x, frame, overlap, fs, win)
  32.  
  33.   % default values
  34.   y = [];
  35.   tscale = [];
  36.  
  37.   % must have at least 3 args
  38.   if (nargs < 4)
  39.   {
  40.     error("sp_stzcr: Requires first four arguments.");
  41.   }
  42.  
  43.   % figure out if we have a vector
  44.   if (min(size(x)) != 1)
  45.   {
  46.     error("sp_stzcr: Input arg \"x\" must be a 1xN or Nx1 vector.");
  47.   }
  48.   
  49.   % work with Nx1 vectors
  50.   x = x[:];
  51.  
  52.   % variables
  53.   Ns = length(x);                             % number of samples
  54.   N = floor(fs * frame);                      % samples per frame
  55.   Ndiff = floor(N * (1 - overlap/100));       % samples between windows
  56.   L = floor((Ns-N)/Ndiff);                    % number of windows
  57.   y = zeros(L,1);                             % space for answer
  58.   tscale = zeros(L,1);                        % space for time
  59.  
  60.   % use the absolute value of x
  61.   t = abs( sgn( x[2:Ns;1] ) - sgn( x[1:Ns-1;1] ) );
  62.  
  63.   % windows with overlap
  64.   for (k in 1:L)
  65.   {
  66.     s1 = (k-1) * Ndiff + 1;                     % start of window
  67.     tscale[k;1] = k * Ndiff/fs;
  68.     y[k;1] = sum(t[s1:s1+N-1;1])/2/N;
  69.   }
  70.  
  71.   return << tscale=tscale ; y=y >>;
  72. };
  73.  
  74.  
  75. % function y = sgn(x)
  76. %
  77. % Returns the signum function.
  78. %
  79. %         { 1    x >= 0
  80. %    y = {
  81. %         {-1    x < 0
  82. %
  83. % by Dennis W. Brown, Last modified: 7/11/93
  84. % Naval Postgraduate School, Monterey, CA
  85.  
  86. sgn = function ( x )
  87. {
  88.   y = sign(x);
  89.   y = (y >= 0) + (-1) * (y == -1);
  90.   return y;
  91. };
  92.